package compilador.util;

import compilador.lexico.Constants;

public enum Tipo {
	
	BOOL("bool", "boolean", "Boolean", Constants.t_boolean, "ldc.i4."),
	STRING("string", "string", "String", Constants.t_const_string, "ldstr "),
	INT("int64", "integer", "Int64", Constants.t_const_integer, "ldc.i8 "),
	FLOAT("float64", "float", "Double64", Constants.t_const_float, "ldc.r8 ");
	
	private String code;
	private String lexeme;
	private String MLISClass;
	private int constCode;
	private String loadCode;
	
	Tipo(String code, String lexeme, String MLISClass, int constCode, String loadCode){
		this.code = code;
		this.lexeme = lexeme;
		this.MLISClass = MLISClass;
		this.constCode = constCode;
		this.loadCode = loadCode;
	}
	
	public String getMLISClass(){
		return this.MLISClass;
	}
	
//	public int getConstCode(){
//		return this.constCode;
//	}
	
	public String getLexeme(){
		return this.lexeme;
	}
	
	public String getLoadCode(){
		return this.loadCode;
	}
	
	public String toString(){
		return this.code;
	}
	
	public boolean isIntOrFloat(){
		return this == INT || this == FLOAT;
	}
	
	public static Tipo getByLexeme(String lexeme){
		Tipo tipo = null;
		Tipo[] tipos = Tipo.values();
		for (int i = 0; i < tipos.length; i++) {
			if(tipos[i].lexeme.equals(lexeme)){
				tipo = tipos[i];
			}
		} 
		
		return tipo;
	}
	
	public static Tipo getByConstCode(int constCode){
		Tipo tipo = null;
		Tipo[] tipos = Tipo.values();
		
		if(constCode == Constants.t_false || constCode == Constants.t_true){
			constCode = Constants.t_boolean;
		}
		
		for (int i = 0; i < tipos.length; i++) {
			if(tipos[i].constCode == constCode){
				tipo = tipos[i];
			}
		} 
		
		return tipo;
	}
}
